//
//  CZTabBar.swift
//  WHWeiBo
//
//  Created by Li Changjian on 16/7/9.
//  Copyright © 2016年 Li Changjian. All rights reserved.
//

import UIKit

class CZTabBar: UITabBar {

    
    // MARK: - 懒加载中间的添加按钮
    lazy var button: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
  
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(button)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        button.centerX = centerX
        button.centerY = height * 0.5

        let itemWidth = width / 5
        var index = 0
        for item in subviews {
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                item.x = CGFloat(index) * itemWidth
                item.width = itemWidth
                if index == 1 {
                    index++
                }
                index++
            }
        
        }
    }
    
}
